import time
from xtquant.xttrader import XtQuantTrader
from xtquant.xttype import StockAccount
from QMT  import MyXtQuantTraderCallback
from 旧版.基础配置 import settings
import asyncio
from xtquant import xtconstant
import json

def save_orders():
    # 初始化交易接口
    path = settings.QMT_DIR
    session_id = int(time.time())
    xt_trader = XtQuantTrader(path, session_id)
    acc = StockAccount(settings.ACC, 'STOCK')
    qmt = MyXtQuantTraderCallback(xt_trader,acc,xt_trader)
    xt_trader.register_callback(qmt)
    xt_trader.start()

    # 连接交易账号
    connect_result = xt_trader.connect()
    if connect_result == 0:
        print('交易连接成功')  # 0=成功
        subscribe_result = xt_trader.subscribe(acc)
        if subscribe_result == 0:
            print("订阅交易回报成功")  # 0=成功
        else:
            print("订阅交易回报失败")  # 0=成功
    else:
        print("交易连接失败，请检查账号或路径！")
        exit()

    stock_code = "513130.SH"
    price = 0.79
    volume = 200
    direction = xtconstant.STOCK_SELL
    order_remark = {
        "buy_id" : 1,
        "buy_type" : "金叉"
    }
    json_string = json.dumps(order_remark, ensure_ascii=False)

    # qmt.place_reverse_order(stock_code, price, volume, direction, json_string)

    qmt.save_orders()
    ##qmt.get_orders()


async def main():
    save_orders()

if __name__ == "__main__":
    asyncio.run(main())